package me.keiwu.user.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import me.keiwu.user.domain.UserForm;

public interface UserFormDao extends JpaRepository<UserForm, Long>, JpaSpecificationExecutor<UserForm> {
	@Query(value = "select * from `user_form` where `user_id` = ?1 and unix_timestamp(now())-created_time/1000<60*60*24*7 order by created_time limit 1", nativeQuery = true)
	UserForm findByUserId(Long userId);

	@Query(value = "delete from `user_form` where unix_timestamp(now())-created_time>=60*60*24*7", nativeQuery = true)
	void deleteExpiredForm();

	@Modifying
	@Query(value = "update `user_form` set `is_del`=1 where `form_id` = ?1", nativeQuery = true)
	void updateUsedFormId(String formId);

}
